package edu.task.service.impl;

import edu.task.mapper.Mapper;
import edu.task.model.SysProject;
import edu.task.model.SysRole;
import edu.task.model.SysUser;
import edu.task.model.SysUserProject;
import edu.task.model.vo.SysProjectVO;
import edu.task.repository.SysProjectRepository;
import edu.task.repository.SysUserProjectRepository;
import edu.task.repository.SysUserRepository;
import edu.task.service.SysProjectService;
import edu.task.utils.BeanUtils;
import edu.task.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class SysProjectServiceImpl implements SysProjectService {

    @Autowired
    SysProjectRepository sysProjectRepository;

    @Autowired
    Mapper mapper;

    @Autowired
    SysUserProjectRepository sysUserProjectRepository;

    @Autowired
    SysUserRepository sysUserRepository;

    @Override
    public List<SysProject> selectProjectList() {
        return sysProjectRepository.findAll();
    }

    @Override
    @Transactional
    public boolean insertProject(SysProjectVO project) {
        SysProject sysProject = new SysProject();
        BeanUtils.copyProperties(project, sysProject);

        sysProject.setStatus("0");
        Date date = new Date();
        sysProject.setCreateTime(date);
        sysProject.setUpdateTime(date);
        for (Long userId : project.getUserIds()) {
            SysUser sysUser = new SysUser();
            sysUser.setUserId(userId);
            sysUser = sysUserRepository.findOne(Example.of(sysUser)).get();
            sysProject.setCreateBy(sysUser.getUserName());
            sysProject.setUpdateBy(sysUser.getUserName());
        }
        sysProjectRepository.save(sysProject);
//        mapper.insertProject(sysProject);

        project.setProjectId(sysProject.getProjectId());
        insertUserProject(project);
        return null != sysProject.getProjectId();
    }

    @Transactional
    public int insertUserProject(SysProjectVO project) {
        Long[] users = project.getUserIds();
        if (StringUtils.isNull(users) || users.length == 0) {
            return 0;
        }
        Long[] projectIds = new Long[users.length];
        Long projectId = project.getProjectId();
        for (Long userId : users) {
            SysUserProject sysUserProject = new SysUserProject();
            sysUserProject.setUserId(userId);
            sysUserProject.setProjectId(projectId);
            sysUserProjectRepository.save(sysUserProject);
        }
        Arrays.fill(projectIds, project.getProjectId());
        return 1;
    }

    @Override
    @Transactional
    public int deleteProjectById(Long projectId) {
        sysUserProjectRepository.deleteUserRoleByProjectId(projectId);
        return sysProjectRepository.deleteProjectById(projectId);
    }

    @Override
    @Transactional
    public boolean updateProject(SysProjectVO project) {
        Long projectId = project.getProjectId();
        Optional<SysProject> op = sysProjectRepository.findById(projectId);
        if (!op.isPresent()) {
            return false;
        }
        // 删除用户与项目关联
        sysUserProjectRepository.deleteUserRoleByProjectId(projectId);
        SysProject sysProject = new SysProject();
        BeanUtils.copyPropertiesIgnoreNull(project, sysProject);
        //更新项目信息
        sysProjectRepository.save(sysProject);
        //新增用户与项目关联
        insertUserProject(project);
        return null != sysProject.getProjectId();
    }

    @Override
    public List<Long> selectProjectListByUserId(Long userId) {
        return sysProjectRepository.selectProjectListByUserId(userId);
    }

    @Override
    public SysProject selectProjectById(Long projectId) {
        SysProject sysProject = new SysProject();
        sysProject.setProjectId(projectId);
        return sysProjectRepository.findOne(Example.of(sysProject)).get();
    }

    @Override
    public List<Long> selectProjectByTaskId(List<Long> taskId) {
        return sysProjectRepository.selectProjectByTaskId(taskId);
    }
}
